﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Helpers;
using System.Data;
using SYcwpb.Access;

namespace SYcwpb.Views
{
    /// <summary>
    /// AddOutAndInDriver.xaml 的交互逻辑
    /// </summary>
    public partial class AddOutAndInDriver : Window
    {
        private SqlDataLayer sqlHelper = new SqlDataLayer(); 
        private int line = Convert.ToInt32(CONSTDATA.UserModel.Line);
        public static AddOutAndInDriver _view = null;
        public List<Driver> list;
        private DriverTeamAccess _teamAccess;
        private UpdateDelegate _de;

        public static AddOutAndInDriver GetInstance(UpdateDelegate de)
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
                _view = new AddOutAndInDriver(de);

            return _view;
        }

        private AddOutAndInDriver(UpdateDelegate de)
        {
            InitializeComponent();
            _de = de;
            _teamAccess = new DriverTeamAccess(Convert.ToInt32(CONSTDATA.UserModel.Line));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            list = InitDriverComboBox();
            cbbdriver.ItemsSource = list;
            cbbdriver.SelectedValuePath = "userid";
            cbbdriver.DisplayMemberPath = "userViewNameAndUserName";
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DateTime time1 = new DateTime();
            DateTime time2 = new DateTime();
            if (!DateTime.TryParse(OutTime.Text.ToString(), out time1))
            {
                MessageBox.Show("出勤时间必须为时间格式");
                return;
            }
            if (!DateTime.TryParse(InTime.Text.ToString(), out time2))
            {
                MessageBox.Show("退勤时间必须为时间格式");
                return;
            }
            if (time1 > time2)
            {
                MessageBox.Show("退勤时间必须大于出勤时间");
                return;
            }
            if (string.IsNullOrEmpty(cbbTeam.Text))
            {
                MessageBox.Show("车队不能为空");
                return;
            }
            if (string.IsNullOrEmpty(cbbdriver.Text))
            {
                MessageBox.Show("添加人员不能为空");
                return;
            }
            Driver driver = list.FirstOrDefault(a => a.userid == cbbdriver.SelectedValue.ToString());
            if (driver == null)
            {
                MessageBox.Show("必须选择一位人员");
                return;
            }
            string date = GetData();
            string atttime = time1.ToString("HH:mm:ss");
            string outtime = time2.ToString("HH:mm:ss");
            string str = "insert into [DriverCombine] ([dcom_lineid],[dcom_team],[dcom_dutynum],[dcom_opdriver],[dcom_monidriver],[dcom_cr_number],[dcom_ybarrive],[dcom_grp_type],[dcom_date],[dcom_monidriverTwo],[dcom_parents],[dcom_NO],[dcom_monidriverThree]) values ('" + CONSTDATA.UserModel.Line + "','" + (cbbTeam.SelectedIndex + 1) + "','0','" + driver.userid + "',NULL,NULL,NULL,'5','" + date + "',NULL,NULL,'0',NULL) select @@identity";
            int id = int.Parse(sqlHelper.GetSingle(str).ToString());
            string str2 = "insert into [DriverGroup] ([dgrp_user_id],[dgrp_line],[dgrp_date],[dgrp_dcom_id],[dgrp_srd_id],[dgrp_attendtime],[dgrp_outtime],[dgrp_room],[replaceDriver],[attenTime],[dgrp_pbyInfo],[dgrp_dtq]) values ('" + driver.userid + "','" + CONSTDATA.UserModel.Line + "','" + date + "','" + id + "',NULL,NULL,NULL,NULL,NULL,'" + atttime + "',NULL,NULL) select @@identity";
            int id2 = int.Parse(sqlHelper.GetSingle(str2).ToString());
            string str3 = "insert into [DriverGroupOutTime] ([DGID],[OutTime],IsTemp) values ('" + id2 + "','" + outtime + "','1')";
            int aaa = sqlHelper.ExecuteSql(str3);
            MessageBox.Show("添加成功");
            DataTable dutyInfoDT = _teamAccess.GetDriverInfoByClass();
            _de(dutyInfoDT);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public List<Driver> InitDriverComboBox()
        {
            List<Driver> lstDrive = new List<Driver>();

            string strSql = "select user_id, user_name,user_view_name,(user_view_name+' '+user_name) as abc from [system_user] where user_dep_id =7 and user_line = " + line + " order by user_view_name";
            DataTable driverDT = sqlHelper.SelectDt(strSql);
            if (driverDT.Rows.Count > 0)
            {
                for (int i = 0; i < driverDT.Rows.Count; i++)
                {
                    Driver driver = new Driver();
                    driver.userName = driverDT.Rows[i]["user_name"].ToString();
                    driver.userViewName = driverDT.Rows[i]["user_view_name"].ToString();
                    driver.userid = driverDT.Rows[i]["user_id"].ToString();
                    driver.userViewNameAndUserName = driverDT.Rows[i]["abc"].ToString();
                    //string item = driver.userViewName + " " + driver.userName;
                    lstDrive.Add(driver);
                }
            }
            return lstDrive;
        }

        public string GetData()
        {
            DateTime dt1 = DateTime.Now;//当前时间
            DateTime dt2 = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 02:00:00");//更新时间
            if (dt1 > dt2)
            {
                return DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                return DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            }
            return DateTime.Now.ToString("yyyy-MM-dd");

        }
    }
}
